package xin.marcher.module.construction.repository;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import org.springframework.stereotype.Repository;
import xin.marcher.module.common.exception.CommonErrorCodeEnum;
import xin.marcher.module.common.exception.ProductBizException;
import xin.marcher.module.construction.converter.CategoryInfoConverter;
import xin.marcher.module.construction.domain.entity.CategoryInfoDO;
import xin.marcher.module.construction.domain.entity.CategoryInfoTreeDO;
import xin.marcher.module.construction.domain.request.CategoryRequest;
import xin.marcher.module.construction.domain.request.QueryCategoryListRequest;
import xin.marcher.module.construction.domain.request.QueryCategoryRequest;
import xin.marcher.module.construction.mapper.CategoryInfoMapper;

import javax.annotation.Resource;
import java.util.List;
import java.util.Objects;

/**
 * 商品审核 资源管理
 */
@Repository
public class CategoryRepository {

    @Resource
    private CategoryInfoMapper categoryInfoMapper;

    @Resource
    private CategoryInfoConverter categoryInfoConverter;

    /**
     * 查询品类信息列表
     *
     * @param categoryListRequest
     * @return
     */
    public List<CategoryInfoDO> selectListByLike(QueryCategoryListRequest categoryListRequest) {
        return categoryInfoMapper.selectListByLike(categoryListRequest);
    }

    /**
     * 查询品类树
     *
     * @param categoryQueryRequest
     * @return
     */
    public List<CategoryInfoTreeDO> selectTree(QueryCategoryRequest categoryQueryRequest) {
        return categoryInfoMapper.selectTree(categoryQueryRequest);
    }

    /**
     * 查询某个层级的类目树
     *
     * @param categoryQueryRequest
     * @return
     */
    public List<CategoryInfoDO> listBy(QueryCategoryRequest categoryQueryRequest) {
        LambdaQueryWrapper<CategoryInfoDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(CategoryInfoDO::getChannel, categoryQueryRequest.getChannel())
                .eq(CategoryInfoDO::getParentId, categoryQueryRequest.getParentId());

        return categoryInfoMapper.selectList(queryWrapper);
    }

    /**
     * 新增或者修改品类树
     *
     * @param categoryRequest
     */
    public void saveOrUpdate(CategoryRequest categoryRequest) {
        if (Objects.isNull(categoryRequest.getId())) {
            save(categoryRequest);
        } else {
            update(categoryRequest);
        }
    }

    private void save(CategoryRequest categoryRequest) {
        CategoryInfoDO categoryInfoDO = categoryInfoConverter.converterDO(categoryRequest);
        // 初始化部分信息
        categoryInfoDO.initCommon();
        // 保存品类信息
        int count = categoryInfoMapper.insert(categoryInfoDO);
        if (count <= 0) {
            throw new ProductBizException(CommonErrorCodeEnum.SQL_ERROR);
        }
    }

    private void update(CategoryRequest categoryRequest) {
        // 保存品类信息
        categoryInfoMapper.updateById(categoryInfoConverter.converterDO(categoryRequest));
    }


}
